package com.xindaoapp.happypet.utils;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;

import com.xindaoapp.happypet.R;

public class CalendarManager {
    private PopupWindow window;
    private String days[];

    /**
     * 生日
     * 
     * @param context
     * @param parentView
     * @param iRequest
     */
    public void getCalendar(final Context context, View parentView, final IRequest<String> iRequest) {
        if (window == null) {
            View view =
                    LayoutInflater.from(context).inflate(R.layout.popuwindow_choice_birthday, null);
            final WheelView year = (WheelView) view.findViewById(R.id.year);
            final String years[] =
                    new String[] {"1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998",
                            "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007",
                            "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016",
                            "2017", "2018", "2019", "2020"};
            year.setVisibleItems(30);
            ArrayWheelAdapter<String> yearAdapter = new ArrayWheelAdapter<String>(context, years);
            yearAdapter.setTextSize(16);
            year.setViewAdapter(yearAdapter);
            year.setCurrentItem(23);

            final WheelView month = (WheelView) view.findViewById(R.id.month);
            final String months[] =
                    new String[] {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11",
                            "12"};
            // 31 28 31 30 31 30 31 31 30 31 30 31
            month.setVisibleItems(12);
            ArrayWheelAdapter<String> monthAdapter = new ArrayWheelAdapter<String>(context, months);
            monthAdapter.setTextSize(16);
            month.setViewAdapter(monthAdapter);
            month.setCurrentItem(5);

            final WheelView day = (WheelView) view.findViewById(R.id.day);
            days = null;
            days =
                    new String[] {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11",
                            "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23",
                            "24", "25", "26", "27", "28", "29", "30"};
            ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<String>(context, days);
            adapter.setTextSize(16);
            day.setViewAdapter(adapter);
            day.setVisibleItems(31);
            day.setCurrentItem(0);
            month.addChangingListener(new OnWheelChangedListener() {
                @Override
                public void onChanged(WheelView wheel, int oldValue, int newValue) {
                    int currentItem = month.getCurrentItem();
                    if (currentItem == 1) {
                        days =
                                new String[] {"01", "02", "03", "04", "05", "06", "07", "08", "09",
                                        "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
                                        "20", "21", "22", "23", "24", "25", "26", "27", "28"};
                        ArrayWheelAdapter<String> adapter =
                                new ArrayWheelAdapter<String>(context, days);
                        adapter.setTextSize(16);
                        day.setViewAdapter(adapter);
                        day.setCurrentItem(days.length / 2);
                    } else if (currentItem == 3 || currentItem == 5 || currentItem == 8
                            || currentItem == 10) {
                        days =
                                new String[] {"01", "02", "03", "04", "05", "06", "07", "08", "09",
                                        "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
                                        "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
                                        "30"};
                        ArrayWheelAdapter<String> adapter =
                                new ArrayWheelAdapter<String>(context, days);
                        adapter.setTextSize(16);
                        day.setViewAdapter(adapter);
                        day.setCurrentItem(days.length / 2);
                    } else {
                        days =
                                new String[] {"01", "02", "03", "04", "05", "06", "07", "08", "09",
                                        "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
                                        "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
                                        "30", "31"};
                        ArrayWheelAdapter<String> adapter =
                                new ArrayWheelAdapter<String>(context, days);
                        adapter.setTextSize(16);
                        day.setViewAdapter(adapter);
                        day.setCurrentItem(days.length / 2);
                    }
                }
            });
            view.findViewById(R.id.popuwindow_cancel).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    window.dismiss();
                }
            });
            view.findViewById(R.id.popuwindow_sure).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    window.dismiss();
                    iRequest.request(years[year.getCurrentItem()] + "-"
                            + months[month.getCurrentItem()] + "-" + days[day.getCurrentItem()]);
                }
            });
            window = new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            window.setAnimationStyle(R.style.PopupAnimation);
            window.setBackgroundDrawable(new BitmapDrawable());
            window.setOutsideTouchable(true);
            window.setFocusable(true);
        }
        window.showAtLocation(parentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL,
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); // 设置layout在PopupWindow中显示的位置
    }

    /**
     * 性别
     * 
     * @param context
     * @param parentView
     * @param iRequest
     */
    public void getsex(final Context context, View parentView, final IRequest<String> iRequest) {
        if (window == null) {
            View view = LayoutInflater.from(context).inflate(R.layout.popuwindow_addpet, null);
            final WheelView year = (WheelView) view.findViewById(R.id.content);
            final String years[] = new String[] {"公", "母"};
            year.setVisibleItems(6);
            ArrayWheelAdapter<String> yearAdapter = new ArrayWheelAdapter<String>(context, years);
            yearAdapter.setTextSize(18);
            year.setViewAdapter(yearAdapter);
            year.setCurrentItem(1);



            view.findViewById(R.id.popuwindow_cancel).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    window.dismiss();
                }
            });
            view.findViewById(R.id.popuwindow_sure).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    window.dismiss();
                    iRequest.request(years[year.getCurrentItem()]);
                }
            });
            window = new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            window.setAnimationStyle(R.style.PopupAnimation);
            window.setBackgroundDrawable(new BitmapDrawable());
            window.setOutsideTouchable(true);
            window.setFocusable(true);
        }
        window.showAtLocation(parentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL,
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); // 设置layout在PopupWindow中显示的位置
    }


    /**
     * 种类
     * 
     * @param context
     * @param parentView
     * @param iRequest
     */
    public void getZhonglei(final Context context, View parentView, final IRequest<String> iRequest) {
        if (window == null) {
            View view = LayoutInflater.from(context).inflate(R.layout.popuwindow_addpet, null);
            final WheelView year = (WheelView) view.findViewById(R.id.content);
            final String[] zhongleis = new String[] {"汪星人", "喵星人", "小宠星人", "水星人"};
            year.setVisibleItems(6);
            ArrayWheelAdapter<String> yearAdapter =
                    new ArrayWheelAdapter<String>(context, zhongleis);
            yearAdapter.setTextSize(16);
            year.setViewAdapter(yearAdapter);
            year.setCurrentItem(1);



            view.findViewById(R.id.popuwindow_cancel).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    window.dismiss();
                }
            });
            view.findViewById(R.id.popuwindow_sure).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    window.dismiss();
                    iRequest.request(zhongleis[year.getCurrentItem()]);
                }
            });
            window = new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            window.setAnimationStyle(R.style.PopupAnimation);
            window.setBackgroundDrawable(new BitmapDrawable());
            window.setOutsideTouchable(true);
            window.setFocusable(true);
        }
        window.showAtLocation(parentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL,
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); // 设置layout在PopupWindow中显示的位置
    }


    /**
     * 种类
     * 
     * @param context
     * @param parentView
     * @param iRequest
     * @param zhonglei
     */
    public void getPinzhong(final Context context, final String[] pinzhongs, View parentView,
            final IRequest<String> iRequest) {
        if (window == null) {

            View view = LayoutInflater.from(context).inflate(R.layout.popuwindow_addpet, null);
            final WheelView year = (WheelView) view.findViewById(R.id.content);
            year.setVisibleItems(6);
            ArrayWheelAdapter<String> yearAdapter =
                    new ArrayWheelAdapter<String>(context, pinzhongs);
            yearAdapter.setTextSize(16);
            year.setViewAdapter(yearAdapter);
            year.setCurrentItem(1);



            view.findViewById(R.id.popuwindow_cancel).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    window.dismiss();
                }
            });
            view.findViewById(R.id.popuwindow_sure).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    window.dismiss();
                    iRequest.request(pinzhongs[year.getCurrentItem()]);
                }
            });
            window = new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            window.setAnimationStyle(R.style.PopupAnimation);
            window.setBackgroundDrawable(new BitmapDrawable());
            window.setOutsideTouchable(true);
            window.setFocusable(true);
        }
        window.showAtLocation(parentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL,
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); // 设置layout在PopupWindow中显示的位置
    }


}
